package org.jeecg.modules.test.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.api.vo.CorrespondingFiledTestClassOneVO;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.test.component.aspect.annotation.CorrespondingFiled;
import org.jeecg.modules.test.entity.CorrespondingFiledTestClassThreeParams;
import org.jeecg.modules.test.entity.CorrespondingFiledTestClassTwoParams;
import org.jeecg.modules.test.entity.HeightLevelBadThing;
import org.jeecg.modules.test.mapper.HeightLevelBadThingMapper;
import org.jeecg.modules.test.service.IHeightLevelBadThingService;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author ZhangYu
 * @date 2023/9/24 23:45
 * @description 测试用户关联坏事
 */
@Slf4j
@Service
public class HeightLevelBadThingServiceImpl extends ServiceImpl<HeightLevelBadThingMapper, HeightLevelBadThing> implements IHeightLevelBadThingService {

    @Resource(name = "threadPoolExecutor")
    private ThreadPoolExecutor threadPoolExecutor;

    @Override
    public List<Integer> testThreadPool() {
        CompletableFuture<List<Integer>> completableFutureOne = CompletableFuture.supplyAsync(this::returnList, threadPoolExecutor);
        CompletableFuture<List<Integer>> completableFutureTwo = CompletableFuture.supplyAsync(this::returnList, threadPoolExecutor);
        CompletableFuture<Void> completableFutureAllOf = CompletableFuture.allOf(completableFutureOne, completableFutureTwo);
        List<Integer> returnList = new ArrayList<>();
        try {
            //等待所有结果完成
            completableFutureAllOf.get();
            returnList.addAll(completableFutureOne.get());
            returnList.addAll(completableFutureTwo.get());
        } catch (InterruptedException | ExecutionException e) {
            log.info("---------------------------------------------------------------------------------------出现错误", e);
            throw new JeecgBootException("出现错误", e);
        }
        return returnList;
    }

    @Override
    public List<Integer> singleThread() {
        List<Integer> returnList = new ArrayList<>();
        returnList.addAll(returnList());
        returnList.addAll(returnList());
        return returnList;
    }

    @NotNull
    private List<Integer> returnList() {
        List<Integer> returnList = new ArrayList<>();
        returnList.add(1);
        log.info("---------------------------------------------------------------------------------结果：{}", returnList);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            log.info("---------------------------------------------------------------------------------------出现错误", e);
            throw new JeecgBootException("出现错误", e);
        }
        return returnList;
    }

    @Override
    public CorrespondingFiledTestClassOneVO moreToSingle(@NotNull CorrespondingFiledTestClassTwoParams correspondingFiledTestClassTwoParams, CorrespondingFiledTestClassThreeParams correspondingFiledTestClassThreeParams) throws Exception {
        CorrespondingFiledTestClassOneVO correspondingFiledTestClassOne = new CorrespondingFiledTestClassOneVO();
        //返回类型
        Class<CorrespondingFiledTestClassOneVO> correspondingFiledTestClassOneClass = CorrespondingFiledTestClassOneVO.class;
        //第一个需要赋值类的属性
        Field[] correspondingFiledTestClassTwoClassFields = correspondingFiledTestClassTwoParams.getClass().getDeclaredFields();
        for (Field field : correspondingFiledTestClassTwoClassFields) {
            field.setAccessible(true);
            //类的值
            Object value = field.get(correspondingFiledTestClassTwoParams);
            extracted(correspondingFiledTestClassOne, correspondingFiledTestClassOneClass, field, value);
        }
        Field[] correspondingFiledTestClassThreeClassFields = correspondingFiledTestClassThreeParams.getClass().getDeclaredFields();
        for (Field field : correspondingFiledTestClassThreeClassFields) {
            //类的值
            field.setAccessible(true);
            Object value = field.get(correspondingFiledTestClassThreeParams);
            extracted(correspondingFiledTestClassOne, correspondingFiledTestClassOneClass, field, value);
        }
        return correspondingFiledTestClassOne;
    }

    private static void extracted(CorrespondingFiledTestClassOneVO correspondingFiledTestClassOne, @NotNull Class<CorrespondingFiledTestClassOneVO> correspondingFiledTestClassOneClass, @NotNull Field field, Object value) throws Exception {
        //获取注解标注的值
        CorrespondingFiled annotation = field.getAnnotation(CorrespondingFiled.class);
        String key = annotation.value();
        //将值赋值到返回类型
        Field correspondingFiledTestClassOneField = correspondingFiledTestClassOneClass.getDeclaredField(key);
        correspondingFiledTestClassOneField.setAccessible(true);
        correspondingFiledTestClassOneField.set(correspondingFiledTestClassOne, value);
    }

}
